<?php

class SiteController extends Controller
{
	

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{   
            //print_r('<pre>');
            //print_r($this->getRules());
            //print_r('</pre>');
            //exit();
            
            $pagevals = array(
                'page_title' => "Trang quản trị",
                'subpage_title' => "Gomo.vn",
                //'action' => 'game/abc'
            );
            $this->display('site/index', array('page_title'=>'Trang quản trị', 'pagevals'=>$pagevals));
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{   
                    //if($error['type'] == 403)
                    $pagevals = array(
                        'page_title' => "Trang quản trị",
                        'subpage_title' => "Gomo.vn",
                        //'action' => 'game/abc'
                    );
                    $this->renderPartial('error',array('pagevals'=>$pagevals,'error'=>$error));
		}
	}

	public function actionLogin()
	{
            $pagevals = array(
                'page_title' => "Trang quản trị",
                'subpage_title' => "Gomo.vn",
                'error'=>'',
                'username'=>'',
                //'action' => 'game/abc'
            );
            if(isset($_POST['username']) && isset($_POST['password'])){
                $user = new UserIdentity($_POST['username'],$_POST['password']);

                if($user->authenticate()){
                     Yii::app()->user->login($user);
                     $this->redirect(Yii::app()->request->urlReferrer);
                }
                else{
                    $pagevals['error'] = $user->errorCode;
                    $pagevals['username']= $_POST['username'];
                }
            }
            
            if(!Yii::app()->user->isGuest){
                $this->redirect('?r=site/index');
            }
            
            $this->renderPartial('login', array('pagevals'=>$pagevals));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
}